<?php

namespace Packages\Authority\Repositories;

use App\Tools\Common;
use Packages\Authority\Models\RelUserRoleModel;
use Packages\Authority\Repositories\BaseRepository;
use Illuminate\Support\Facades\DB;
use Exception;

class RelUserRoleRepositories
{
    use BaseRepository;

    protected static $db;

    public function __construct()
    {
        self::$db = new RelUserRoleModel;
    }

    /**
     * 获取全部
     * @param $data
     * @return array
     */
    public function all($data)
    {

        // 查询条件
        $result = self::$db
            ->when(@$data['select'], function ($q) use ($data) {
                $q->selectRaw($data['select']);
            })
            ->when(@$data['guid'], function ($q) use ($data) {
                $q->where('guid', $data['guid']);
            })
            ->when(@$data['status'], function ($q) use ($data) {
                $q->where('status', $data['status']);
            })
            ->when(@$data['platform'], function ($q) use ($data) {
                $q->where('platform', $data['platform']);
            })
            ->when(@$data['name'], function ($q) use ($data) {
                $q->where('name', 'like', '%' . $data['name'] . '%');
            })
            ->when(@$data['app_id'], function ($q) use ($data) {
                $q->where('app_id', $data['app_id']);
            })
            ->when(@$data['limit'], function ($q) use ($data) {
                $q->limit($data['limit']);
            })
            ->orderBy('created_at', 'desc')
            ->get();

        return $result;
    }

    /**
     * 获取列表
     * @param $data
     * @return array
     */
    public function list($data)
    {
        $current = $data['current'] ?? 1;
        $pageSize = $data['pageSize'] ?? 10;
        $order = $data['order'] ?? 'created_at';
        $by = $data['by'] ?? 'desc';

        // 查询条件
        $paginate = self::$db
            ->when(@$data['guid'], function ($q) use ($data) {
                $q->where('guid', $data['guid']);
            })
            ->when(@$data['status'], function ($q) use ($data) {
                $q->where('status', $data['status']);
            })
            ->when(@$data['platform'], function ($q) use ($data) {
                $q->where('platform', $data['platform']);
            })
            ->when(@$data['name'], function ($q) use ($data) {
                $q->where('name', 'like', '%' . $data['name'] . '%');
            })
            ->when(@$data['app_id'], function ($q) use ($data) {
                $q->where('app_id', $data['app_id']);
            })
            ->orderBy($order, $by)
            ->paginate($pageSize, ['*'], 'page', $current);

        return [
            'current' => $current,
            'data' => $paginate->items(),
            'pageSize' => $pageSize,
            'total' => $paginate->total(),
        ];
    }

    /**
     * 添加
     * @param array $data 
     * @return collection
     */
    public function create(array $data)
    {

        $info = self::$db->create([
            'user_id' => $data['user_id'], // 用户ID
            'role_id' => $data['role_id'], // 角色ID
            'platform' => $data['platform'] ?? 'admin', // 平台
            'app_id' => $data['app_id'] ?? '0' // 应用ID
        ]);

        return $info;
    }
}
